package gamelogic

import (
	"server_cluster/common/config"
	"server_cluster/common/gdconf"
	"server_cluster/common/logger"
	"server_cluster/common/mysock"
	"server_cluster/common/protoc/pb"
	"server_cluster/logic/sharemem"
)

func ShopBuy(goodsId int32, count int32, user *sharemem.User) (int16, *pb.Item) {
	shopConfig := gdconf.GetShopConfig(goodsId)
	if shopConfig == nil {
		return config.CONFIG_NOT_FOUND, nil
	}

	now := sharemem.MyShm.TimeNow
	if shopConfig.StartTime != nil && now < shopConfig.StartTime.Unix() {
		logger.Error("shop goods start time not arrive, goodsId: %v, uid: %v", goodsId, user.GetUid())
		return config.SERVER_ERROR, nil
	}
	if shopConfig.EndTime != nil && now > shopConfig.EndTime.Unix() {
		logger.Error("shop goods end time is gone, goodsId: %v, uid: %v", goodsId, user.GetUid())
		return config.SERVER_ERROR, nil
	}

	switch shopConfig.UnlockType {
	case gdconf.ShopUnlockTypePlayerLevel:
		if int32(user.Level) < shopConfig.UnlockParam {
			logger.Error("player level < shop goods unlock player level, curr: %v, need: %v, uid: %v", user.Level, shopConfig.UnlockParam, user.GetUid())
			return config.SERVER_ERROR, nil
		}
	}

	if shopConfig.BuyLimitType != gdconf.ShopBuyLimitTypeNone {
		buyCount := user.GetBuyShopRecord(shopConfig.ShopGoodsId)
		remainCount := shopConfig.BuyLimitCount - buyCount
		if remainCount < count {
			logger.Error("shop goods remain count < buy count, remain: %v, buy: %v, uid: %v", remainCount, count, user.GetUid())
			return config.SERVER_ERROR, nil
		}
	}

	ok := CostItems(map[int32]int32{shopConfig.CostItem.ItemId: shopConfig.CostItem.ItemCount * count}, user)
	if !ok {
		return config.ITEM_NOT_ENOUGH, nil
	}
	AddItems(map[int32]int32{shopConfig.ItemId: shopConfig.ItemCount * count}, user)
	user.AddShopRecord(goodsId, count)
	return config.OK, &pb.Item{Id: shopConfig.ItemId, Num: shopConfig.ItemCount * count}
}

//============消息协议处理==========

func ShopDataReq(user *sharemem.User, buf *mysock.SockBuf) int16 {
	req, ok := ParseMsg(config.CS_ShopDataReq, new(pb.CS_ShopDataReq), buf, user).(*pb.CS_ShopDataReq)
	if !ok {
		return config.PROTO_ERROR
	}
	_ = req

	user.RefreshShop()
	goodsList := make([]*pb.ShopGoods, 0)
	for goodsId := range gdconf.GetShopConfigMap() {
		goodsList = append(goodsList, sharemem.PacketShopGoods(goodsId, user))
	}

	return SendMsg(config.SC_ShopDataRsp, &pb.SC_ShopDataRsp{RetCode: config.OK, GoodsList: goodsList}, buf, user)
}

func ShopBuyReq(user *sharemem.User, buf *mysock.SockBuf) int16 {
	req, ok := ParseMsg(config.CS_ShopBuyReq, new(pb.CS_ShopBuyReq), buf, user).(*pb.CS_ShopBuyReq)
	if !ok {
		return config.PROTO_ERROR
	}

	user.RefreshShop()
	ret, item := ShopBuy(req.GoodsId, req.Count, user)
	if ret != config.OK {
		return SendMsg(config.SC_ShopBuyRsp, &pb.SC_ShopBuyRsp{RetCode: int32(ret)}, buf, user)
	}

	return SendMsg(config.SC_ShopBuyRsp, &pb.SC_ShopBuyRsp{
		RetCode: config.OK,
		Goods:   sharemem.PacketShopGoods(req.GoodsId, user),
		Item:    item,
	}, buf, user)
}
